package com.adams.app.listener;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.adams.app.AppData;
import com.adams.app.R;
import com.adams.app.common.Global;
import com.baidu.mapapi.map.MKEvent;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.PoiOverlay;
import com.baidu.mapapi.map.PopupClickListener;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.map.TransitOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;

public class MySearchListener implements MKSearchListener {

	private Handler handler;
	private Activity context;
	private MapView mapView;
	private static final String TAG = "MySearchListener";
	
	
	public MySearchListener() {
		
	}

	public MySearchListener(Activity context, MapView mMapView, Handler handler) {
		this.context = context;
		this.mapView = mMapView;
		this.handler = handler;
	}

	public MySearchListener(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void onGetAddrResult(MKAddrInfo res, int error) {
		if (error != 0) {
			Log.i(TAG, String.valueOf(error));
			return;
		}
		if (res == null || res.geoPt == null) return;
	
		GeoPoint point = res.geoPt;
		
		mapView.refresh();
		mapView.getController().animateTo(point);
		
	
		Message msg = new Message();
		msg.what = Global.STOP_POS;
		msg.obj = res.geoPt;
		handler.sendMessage(msg);
	}

	@Override
	public void onGetPoiResult(MKPoiResult res, int type, int error) {
		if (error == MKEvent.ERROR_RESULT_NOT_FOUND) {
			return;
		} else if (error != 0 || res == null) {
			return;
		}
		
		if (AppData.flag.equals(Global.CITY_NAME)) {
			Log.i("TEST", "app1");
			PoiOverlay poiOverlay = new PoiOverlay((Activity) context, mapView);
			ArrayList<MKPoiInfo> infos = new ArrayList<MKPoiInfo>();
			infos.add(res.getPoi(0));

			poiOverlay.setData(infos);
			mapView.getOverlays().clear();
			mapView.getOverlays().add(poiOverlay);
			mapView.refresh();

			// 当ePoiType为2（公交线路）或4（地铁线路）时， poi坐标为空
			for (MKPoiInfo info : res.getAllPoi()) {
				if (info.pt != null) {
					mapView.getController().animateTo(info.pt);
					break;
				}
			}

		} else  {
			Log.i("TEST", "app2");
			ArrayList<MKPoiInfo> list = res.getAllPoi();
			Message msg2 = new Message();
			msg2.what = Global.WHAT1;
			msg2.obj = list;
			handler.sendMessage(msg2);
		}
	}

	@Override
	public void onGetDrivingRouteResult(MKDrivingRouteResult result, int arg1) {
		Log.i("Map", "驾车");
		if (result == null) {
			Log.i("TEST", "nonono");
			return;
		}
		
		
		RouteOverlay routeOverlay = new RouteOverlay(context, mapView); // 此处仅展示一个方案作为示例
		routeOverlay.setData(result.getPlan(0).getRoute(0));
		mapView.getOverlays().clear();
		mapView.getOverlays().add(routeOverlay);
		mapView.refresh();

	}

	@Override
	public void onGetWalkingRouteResult(MKWalkingRouteResult result, int arg1) {
		if (result == null) {
			return;
		}
		
		RouteOverlay routeOverlay = new RouteOverlay(context, mapView);
		routeOverlay.setData(result.getPlan(0).getRoute(0));
		mapView.getOverlays().clear();
		mapView.getOverlays().add(routeOverlay);
		mapView.refresh();
	}

	@Override
	public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
		if (result == null) {
			return;
		}
		
		TransitOverlay transitOverlay = new TransitOverlay(context, mapView); 
		transitOverlay.setData(result.getPlan(0));
		mapView.getOverlays().clear();
		mapView.getOverlays().add(transitOverlay);
		mapView.refresh();
	}

	
	
	
	
	
	public void onGetBusDetailResult(MKBusLineResult result, int iError) {

	}

	@Override
	public void onGetPoiDetailSearchResult(int arg0, int error) {

	}

	@Override
	public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1, int arg2) {

	}

	@Override
	public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {

	}

}
